﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
//using System.Web.Security;
using System.Security.Cryptography.X509Certificates;

namespace Haozes.FxClient.CommUtil
{
    public static class HttpHelper
    {
        public static string Post(string url, string data)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            string responseHTML = string.Empty;

            byte[] replybyte = Encoding.UTF8.GetBytes(data);
            request = (HttpWebRequest)WebRequest.Create(url);
            request.UserAgent = "IIC2.0/PC 3.3.0370";
            request.Method = "POST";
            //post 开始
            request.ContentLength = data.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(replybyte, 0, replybyte.Length);
            newStream.Close();
            //post 结束

            //返回HTML
            response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("utf-8"));
            responseHTML = reader.ReadToEnd();
            return responseHTML;
        }


        public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
        {
            public bool CheckValidationResult(ServicePoint sp, X509Certificate cert, WebRequest req, int problem)
            {
                return true;
            }
        }

        /// <summary>
        /// 返回包含ssic和http response内容两个元素的数组
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="data">The data.</param>
        /// <returns>string[]</returns>
        public static string[] Get(string url, string data)
        {
            string[] list = new string[2];
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            string responseHTML = string.Empty;

            byte[] replybyte = Encoding.UTF8.GetBytes(data);
            ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
            //ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
            request = (HttpWebRequest)WebRequest.Create(url);

            request.UserAgent = "IIC2.0/PC 3.3.0370";
            request.Method = "GET";

            //返回HTML
            response = (HttpWebResponse)request.GetResponse();
            string cookieStr = response.Headers["Set-Cookie"];
            string ssic = cookieStr.Split(';')[0].Trim();
            list[0] = ssic;
            //CookieCollection cookies = req.Cookies;
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("utf-8"));
            responseHTML = reader.ReadToEnd();
            list[1] = responseHTML;

            return list;
        }

        //public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        //{   //   Always   accept   
        //    return true;
        //}
    }
}
